import { Injector } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog } from '@angular/material';
import { map, tap } from 'rxjs/operators';
import { get } from 'lodash';
import { SweetAlertService } from '../share/services/sweet-alert.service';
import { ToasterService } from '../share/services/toaster.service';
import { FormBuilder } from '@angular/forms';

export class DetailBase {
	constructor(
		private _inject: Injector,
		protected router = _inject.get(Router),
		protected route = _inject.get(ActivatedRoute),
		public dialog = _inject.get(MatDialog),
		protected sweet = _inject.get(SweetAlertService),
		protected toaster = _inject.get(ToasterService),
		protected fb = _inject.get(FormBuilder),
	) {}
	reload() {
		this.router.navigated = false;
		this.router.navigate([], { queryParamsHandling: 'merge' });
	}

	getData<T>(path: string) {
		return this.route.data.pipe(
			tap(console.log),
			map(data => get(data, path) as T),
		);
	}

	

	back(opt = { level: 2 }) {
		this.router.navigate([new Array(opt.level).fill('..').join('/')], { relativeTo: this.route });
	}
}
